package by.amakarevich.webapp.page.edit;

import java.util.Arrays;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.EnumChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.amakarevich.datamodel.Kit;
import by.amakarevich.datamodel.Product;
import by.amakarevich.datamodel.Tambour;
import by.amakarevich.datamodel.Thread;
import by.amakarevich.datamodel.enums.Artifact;
import by.amakarevich.datamodel.enums.Category;
import by.amakarevich.services.KitService;
import by.amakarevich.services.ProductService;
import by.amakarevich.services.TambourService;
import by.amakarevich.services.ThreadService;
import by.amakarevich.webapp.page.AlternativeBaseLayout;
import by.amakarevich.webapp.page.edit.kit.KitCreatePage;
import by.amakarevich.webapp.page.edit.tambour.TambourCreateEditPage;
import by.amakarevich.webapp.page.edit.thread.ThreadCreateEditPage;
import by.amakarevich.webapp.page.home.HomePage;

public class ProductCreateEditPage extends AlternativeBaseLayout {
	@Inject
	private ProductService productService;
	@Inject
	private KitService kitService;
	@Inject
	private TambourService tambourService;
	@Inject
	private ThreadService threadService;

	private Kit kit;
	private Tambour tambour;
	private Thread thread;

	public ProductCreateEditPage(final Product product) {
		super();
		Form<Product> form = new Form<Product>("form",
				new CompoundPropertyModel<Product>(product));

		final TextField<String> tfItem = new TextField<String>("item");
		tfItem.add(new PropertyValidator<String>());
		tfItem.setLabel(new ResourceModel("p.product.item"));
		form.add(tfItem);

		final TextField<String> tfCompany = new TextField<String>("company");
		tfCompany.add(new PropertyValidator<String>());
		tfCompany.setLabel(new ResourceModel("p.product.company"));
		form.add(tfCompany);

		final TextField<Long> tfPrice = new TextField<Long>("price");
		tfPrice.add(new PropertyValidator<Long>());
		tfPrice.setLabel(new ResourceModel("p.product.price"));
		form.add(tfPrice);

		final TextField<Long> tfAvailable = new TextField<Long>("available");
		tfAvailable.add(new PropertyValidator<Long>());
		tfAvailable.setLabel(new ResourceModel("p.product.available"));
		form.add(tfAvailable);

		final TextField<Long> tfRequiredNumber = new TextField<Long>(
				"requiredNumber");
		tfRequiredNumber.add(new PropertyValidator<Long>());
		tfRequiredNumber.setLabel(new ResourceModel("p.product.required"));
		form.add(tfRequiredNumber);

		final TextField<Long> tfOrdered = new TextField<Long>("ordered");
		tfOrdered.add(new PropertyValidator<Long>());
		tfOrdered.setLabel(new ResourceModel("p.product.ordered"));

		form.add(tfOrdered);

		form.add(new DropDownChoice<Artifact>("artifact", Arrays
				.asList(Artifact.values()), new EnumChoiceRenderer<Artifact>(
				this)));

		form.add(new DropDownChoice<Category>("category", Arrays
				.asList(Category.values()), new EnumChoiceRenderer<Category>(
				this)));

		form.add(new SubmitLink("sumbit-link") {
			@Override
			public void onSubmit() {
				super.onSubmit();

				if (product.getCategory().equals(Category.kit)) {

					if (product.getId() != null) {
						productService.saveOrUpdate(product);
						kit = kitService.getByProductId(product);
						setResponsePage(new KitCreatePage(kit, product));
					} else {
						setResponsePage(new KitCreatePage(new Kit(), product));
					}
				} else {
					if (product.getCategory().equals(Category.tambour)) {

						if (product.getId() != null) {
							productService.saveOrUpdate(product);
							tambour = tambourService.getByProductId(product);
							setResponsePage(new TambourCreateEditPage(tambour, product));
						} else {
							setResponsePage(new TambourCreateEditPage(new Tambour(),
									product));
						}
					} else {
						if (product.getCategory().equals(Category.thread)) {

							if (product.getId() != null) {
								productService.saveOrUpdate(product);
								thread = threadService.getByProductId(product);
								setResponsePage(new ThreadCreateEditPage(thread, product));
							} else {
								setResponsePage(new ThreadCreateEditPage(new Thread(),
										product));
							}
						} else {
							setResponsePage(new HomePage());
						};
					};	
				};
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);

	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("p.create.title");
	}

}
